package com.gollum.jpa.enums;



import com.gollum.jpa.util.StrUtils;

import javax.persistence.criteria.Predicate;

/**
 * 查询链规则
 *
 * @author Herther
 * @version 1.0.0
 * @ClassName MatchTypeEnum.java
 * @createTime 2022年07月17日 00:00:00
 */
public enum SuperQueryMatch {

    AND("AND"),
    OR("OR");

    private String value;

    SuperQueryMatch(String value) {
        this.value = value;
    }


    public static SuperQueryMatch getByValue(Object value) {
        if (StrUtils.isEmpty(value)) {
            return null;
        }
        return getByValue(value.toString());
    }

    public static SuperQueryMatch getByValue(String value) {
        if (StrUtils.isBlank(value)) {
            return null;
        }
        for (SuperQueryMatch val : values()) {
            if (val.getValue().toLowerCase().equals(value.toLowerCase())) {
                return val;
            }
        }
        return null;
    }

    public static Predicate.BooleanOperator getQueryMatch(String matchType){
        Predicate.BooleanOperator operator =  Predicate.BooleanOperator.AND;
        matchType = matchType.toUpperCase();
        if (matchType.equals(SuperQueryMatch.OR.getValue())){
            operator = Predicate.BooleanOperator.OR;
        }
        return operator;
    }

    public String getValue() {
        return value;
    }

}
